﻿using Max.PH4.Shoppgin.Admin.DTO;
using Max.PH4.Shoppgin.Admin.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH4.Shoppgin.Admin.API.Controllers.Product
{
    [Route("category")]
    [ApiController]
    public class CategoryController : ControllerBase
    {
        ICategoryService _service;
        NLog.Logger _log;

        public CategoryController(ICategoryService service)
        {
            _service = service;
            _log = NLog.LogManager.GetCurrentClassLogger();
        }

        /// <summary>
        /// 递归分类
        /// </summary>
        /// <returns></returns>
        [HttpGet("recursion")]
        public IActionResult GetRecursionCategory()
        {
            ResponseResultDto result = new();
            try
            {
                var data = _service.GetRecursionCategory();

                result.Data = data;
            }
            catch (Exception ex)
            {
                result.Code = ResponseStateCode.Failed;
                result.Message = ex.Message;
                _log.Error($"分类递归接口出错啦，原因：{ex.Message}");
            }
            return Ok(result);

        }

        /// <summary>
        /// 分类管理
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCategory(int pageIndex = 1,int pageSize = 10)
        {
            ResponseResultDto result = new();
            try
            {
                var data = _service.GetRecursionCategory();
                var total = data.Count();

                result.Data = new { items = data, total };
            }
            catch (Exception ex)
            {
                result.Code = ResponseStateCode.Failed;
                result.Message = ex.Message;
                _log.Error($"分类列表接口出错啦，原因：{ex.Message}");
            }
            return Ok(result);
        }
    }
}
